package com.yx.frame.web;

import java.io.File;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

import com.yx.frame.http.HttpFileDownloadServletJre6;
import com.yx.frame.http.HttpFileUploadServletJre6;
import com.yx.frame.http.HttpJsonRemoteServletJre6;

public class WebServer {
	public static final String fileUploadTempDir="./uploadtmp/";
	public static final int fileSizeThreshold = 1024*1024*2;
	public static long maxFileSize = 1024*1024*50;
	//public static long maxRequestSize =  1024*1024*100 ;
	private int minThreadPoolSize=15;
	private int maxThreadPoolSize=30;
	private int port;
	public WebServer(int port,int minThreadPoolSize,
			int maxThreadPoolSize) {
		this.port=port;
		this.minThreadPoolSize=minThreadPoolSize;
		this.maxThreadPoolSize=maxThreadPoolSize;
		File _fileUploadTempDir=new File(fileUploadTempDir);
		if(!_fileUploadTempDir.exists()){
			_fileUploadTempDir.mkdirs();
		}
	}
	public void start(boolean isJDK18) {
		QueuedThreadPool threadPool = new QueuedThreadPool(maxThreadPoolSize);
		threadPool.setMinThreads(minThreadPoolSize);
		Server server = new Server();	
		server.setThreadPool(threadPool);
		Connector  httpConnector = new SelectChannelConnector();
		httpConnector.setServer(server);
		httpConnector.setPort(port);
		WebAppContext webAppContext = new WebAppContext("WebContent", "/");
		webAppContext.setContextPath("/");
		webAppContext.setResourceBase("./web/");
		webAppContext.setDisplayName("SmartBuilder");
		webAppContext.setClassLoader(Thread.currentThread().getContextClassLoader());
		webAppContext.setConfigurationDiscovered(true);
		webAppContext.setParentLoaderPriority(true);
		server.setConnectors(new Connector[]{ httpConnector });
		server.setHandler(webAppContext);
		if(isJDK18){
//			ServletHolder httpJsonRemoteServlet= new ServletHolder(new HttpJsonRemoteServlet());
//			httpJsonRemoteServlet.getRegistration().setAsyncSupported(true);
//			webAppContext.addServlet(httpJsonRemoteServlet, "/JsonRemote");		
//			ServletHolder httpFileUploadServlet= new ServletHolder(new HttpFileUploadServlet());
//			httpFileUploadServlet.getRegistration().setAsyncSupported(true);		
//			httpFileUploadServlet.getRegistration().setMultipartConfig(new MultipartConfigElement(fileUploadTempDir, maxFileSize, maxFileSize, fileSizeThreshold));
//			webAppContext.addServlet(httpFileUploadServlet, "/FileUpload"); 
//			ServletHolder httpFileDownloadServlet= new ServletHolder(new HttpFileDownloadServlet());
//			httpFileDownloadServlet.getRegistration().setAsyncSupported(true);
//			webAppContext.addServlet(httpFileDownloadServlet, "/Download"); 		
		}else{
			ServletHolder httpJsonRemoteServlet= new ServletHolder(new HttpJsonRemoteServletJre6());
			webAppContext.addServlet(httpJsonRemoteServlet, "/JsonRemote");		
			ServletHolder httpFileUploadServlet= new ServletHolder(new HttpFileUploadServletJre6());			
			webAppContext.addServlet(httpFileUploadServlet, "/FileUpload"); 
			ServletHolder httpFileDownloadServlet= new ServletHolder(new HttpFileDownloadServletJre6());
			webAppContext.addServlet(httpFileDownloadServlet, "/Download"); 		
		}		
		try {
			server.start();
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("server is  start");
	}	
}
